﻿#include "AppManager.h"
#include <QMainWindow>
#include <QApplication>
#include <QStyleFactory>
#include <QFile>
#include <QTranslator>
#include <QDebug>
#include "IBaseSignal.h"
#include "QBaseLog.h"

namespace BaseModule
{
    AppManager::AppManager()
    {
        auto _baseSignal = IBaseSignalInstance;

        connect(_baseSignal, &BaseModule::IBaseSignal::signal_sendQuestionMessageToUI, this, &AppManager::slot_sendQuestionMessageToUI);
        connect(_baseSignal, &BaseModule::IBaseSignal::signal_sendErrorMessageToUI, this, &AppManager::slot_sendErrorMessageToUI);
    }

    AppManager::~AppManager()
    {
    }

    void AppManager::init(QMainWindow* mainWindow)
    {
        m_mainWindow = mainWindow;

        // 将打印到控制台的信息转存到日志文件中，release模式有效
#ifdef NDEBUG
        if (!QBaseLog::open_sys_message(qApp->applicationDirPath() + "/log"))
        {
            printf("create log file fail");
            slot_sendErrorMessageToUI(QStringLiteral("创建日志文件失败."));
            return;
        }
        qInstallMessageHandler(QBaseLog::SysMessageOutput);
        qDebug() << "\n\n========================The program start running========================";
#endif

        // 初始化界面样式
        initAppStyle();

        // 翻译
        QTranslator* pTranslator = new QTranslator();
        if (pTranslator->load(":/translations/qt_zh_CN.qm"))
        {
            qApp->installTranslator(pTranslator);
        }

        // 窗口关闭事件统一处理
        handleAppClose();

    }

    void AppManager::initAppStyle()
    {

        QStringList styles = QStyleFactory::keys();
        if (styles.contains("Fusion"))
        {
            qApp->setStyle("Fusion");
        }

        // 加载样式文件
        QFile _fileQss(":/lightstyle.qss");
        if (_fileQss.open(QFile::ReadOnly | QFile::Text))
        {
            QTextStream stream(&_fileQss);
            qApp->setStyleSheet(stream.readAll());
            _fileQss.close();
        }

    }


    /// @brief 获取程序文件目录路径

    QString AppManager::getAppPath()
    {
        return qApp->applicationDirPath();
    }

    /// @brief 获取程序安装目录路径

    QString AppManager::getInstallPath()
    {
        auto _appPath = qApp->applicationDirPath();
        return _appPath.mid(0, _appPath.lastIndexOf('/'));
    }

    /// @brief 获取是否是界面模式

    bool AppManager::getIsGUI()
    {
        return true;
    }

    void AppManager::slot_sendQuestionMessageToUI(int& result, const QString& title, const QString& message)
    {
        //auto _closeDialog = new ElaContentDialog(m_mainWindow);
        //_closeDialog->setTitleText(title);
        //_closeDialog->setContentText(message);
        //_closeDialog->setUseMultilineText(true);
        //
        //connect(_closeDialog, &ElaContentDialog::leftButtonClicked, [=]() { _closeDialog->reject(); }); //取消
        //connect(_closeDialog, &ElaContentDialog::rightButtonClicked, [=]() { _closeDialog->accept(); });    //确定
        //
        //result = _closeDialog->exec();
    }

    void AppManager::slot_sendErrorMessageToUI(const QString& message)
    {
        //auto _closeDialog = new ElaContentDialog(m_mainWindow);
        //_closeDialog->setTitleText(QStringLiteral("错误"));
        //_closeDialog->setContentText(message);
        //_closeDialog->setUseMultilineText(true);
        //_closeDialog->setLeftButtonVisible(false);
        //_closeDialog->exec();
    }

    void AppManager::handleAppClose()
    {

        //auto _mainWindow = qobject_cast<ElaWindow*>(m_mainWindow);
        //// 拦截默认关闭事件
        //auto _closeDialog = new ElaContentDialog(m_mainWindow);
        //_closeDialog->setContentText(QStringLiteral("确定要退出程序吗？"));
        //_closeDialog->setMiddleButtonVisible(true);
        //connect(_closeDialog, &ElaContentDialog::middleButtonClicked, _mainWindow, &ElaWindow::showMinimized);
        //connect(_closeDialog, &ElaContentDialog::rightButtonClicked, _mainWindow, &ElaWindow::closeWindow);
        //_mainWindow->setIsDefaultClosed(false);
        //connect(_mainWindow, &ElaWindow::closeButtonClicked, this, [=]() {
        //    bool isClose = true; //true允许，false禁止
        //    emit IBaseSignalInstance->signal_readyCloseApp(isClose);
        //    if (isClose)
        //    {
        //        _closeDialog->exec();
        //        QBaseLog::close_sys_message();
        //    }
        //    });
    }


}
